var _flag =''; //用来记录是新增界面的机构还是修改界面的机构

var showTr = {
		closeloading:function(){
			$("#orgloading").hide();
		},
		showloading:function(){
			$("#orgloading").show();
			$("#msg_dialog_loading").hide();
		}
}
function treeFresh(){
	queryOrgsAjax.send();
}
var btnEvents = {
	query:function(){
		var frmObj = $("#staffQueryFrm").serializeObject();
		var selectTreeNodes = orgsStaffTree.getCheckNodes(true);
		var orgIds = new Array();
		for(var i=0;i<selectTreeNodes.length;i++){
			if(selectTreeNodes[i].children == null ||
					selectTreeNodes[i].children.length == 0){
				orgIds.push(selectTreeNodes[i].orgId);
			}
		}
		$("#staffQueryFrm input[name='orgIds']").val(orgIds);
		$("#staffQueryFrm").submit();
	},
	reset:function(){
		orgsStaffTree.checkAllNodes(false);
		$("#staffQueryFrm")[0].reset();
	},
	onAddStaff:function(){
       /* var selectTreeNodes = orgsStaffTree.getCheckNodes(true);

        var orgIds = new Array();
        var orgName = new Array();
        for(var i=0;i<selectTreeNodes.length;i++){
            if(selectTreeNodes[i].children == null ||
                selectTreeNodes[i].children.length == 0){
                orgIds.push(selectTreeNodes[i].orgId);
                orgName.push(selectTreeNodes[i].orgName)
            }
        }*/
        $('#createStaffFrm')[0].reset();
        MsgBox.open("onAddStaff", "新增员工信息", ctx + "/sys/staffMgr/add/Index", "600px", null, true);
        //MsgBox.trigger("onAddStaff", "", "#createStaffDialog");
       // getPlatCode.send();
    },
    commitAddStaff:function () {
        var staffName = $.trim($("#staffName1").val());
        if(!staffName){
            MsgBox.info("员工姓名不能为空",null,function(){
            });
            return false;
        }
        // 密码不能为空
        var staffPwd = $.trim($("#staffPwd1").val());
        if(!staffPwd){
            MsgBox.info("登录密码不能为空",null,function(){
            });
            return false;
		}

		//员工编号不能为空
		var  staffId = $.trim($("#staffId1").val());
        if(!staffId){
            MsgBox.info("用户编号不能为空",null,function(){
            });
            return false;
		}
        var a = 0;
        if(a = 0 ){
        	a++;
            $("#createStaffFrm").submit();
        }
        /*var newStaff = $("#createStaffFrm").serializeObject();

        addStaffAjax.setData(newStaff);
        addStaffAjax.send();
        staffInfoDataTable.refresh();*/
    },
    onDelStaff:function(){
        var selectRecords = staffInfoDataTable.getSelectedRecords();
        if(selectRecords == null || selectRecords.length == 0){
            MsgBox.warn("请选择一条记录"); return ;
        }else{
            MsgBox.confirm("确认删除所选信息？","删除确认",function(){
                delStaffAjax.addParam("staffId",selectRecords[0].data.staffId);
                delStaffAjax.send();
            },null);
        }
    },
    onUpdateStaff:function(){
		/*$('#createChannelFrm')[0].reset();*/
        $("#updateStaffFrm label").closest('label.has-error').remove();
        $("#updateStaffFrm label").closest('div.form-group').removeClass("has-error");
        Logger.info("update staff Info");
        var curr = staffInfoDataTable.getCurrentRecord();
        if(curr == null){
            MsgBox.warn("请选择一条记录"); return ;
        }
        $("#updateStaffFrm")[0].reset();
        $("#updateStaffFrm").autofill(curr.toJson());
        MsgBox.open("onEditStaff", "修改员工信息", ctx + "/sys/staffMgr/update/"+curr.toJson().staffId+"/Index", "600px", null, true);
        //MsgBox.trigger("onEditStaff", "", "#updateStaffDialog");
    },
    commitUpdateStaff:function(){
        var staffName = $.trim($("#staffName2").val());
        if(!staffName){
            MsgBox.info("员工姓名不能为空",null,function(){
            });
            return false;
        }
        var a = 0;
        if(a = 0 ){
        	a++;
        	$("#updateStaffFrm").submit();
        }
        //var newStaff = $("#updateStaffFrm").serializeObject();

        //updateStaffAjax.setData(newStaff);
        //updateStaffAjax.send();
        //var mWindow = MsgBox.getCurrentOpenDialogWindow();
        //mWindow.staffInfoDataTable.refresh();
        //staffInfoDataTable.refresh();

	},
	alertTree:function(flag){
    	_flag=flag;
    	MsgBox.open("onAlertTree", "选择机构", ctx + "/sys/staffMgr/choose/Index", "400px");
        //MsgBox.trigger("onAlertTree", "", "#alertTreeDialog");
	},
    commitOrg:function () {
		 var selectTreeNodes = orgsStaffTree1.getCheckNodes(true);

		 var orgIds = new Array();
		 var orgName = new Array();
		 for(var i=0;i<selectTreeNodes.length;i++){
		 if(selectTreeNodes[i].children == null ||
		 selectTreeNodes[i].children.length == 0){
		 orgIds.push(selectTreeNodes[i].orgId);
		 orgName.push(selectTreeNodes[i].orgName)
		 }
		 }

        if(orgIds.length>1){
            MsgBox.warn("只能选择一个机构"); return ;
        }
        $("#staffNetId1").val(orgIds[0]);
        $("#staffNetName1").val(orgName[0]);
        $("#staffNetId2").val(orgIds[0]);
        $("#staffNetName2").val(orgName[0]);
        orgsStaffTree1.checkAllNodes(false);
        $('#alertTreeDialog').modal('hide')

    }
};
var tableO = { //角色表事件
		onRoleRenderItem:function(colName,text){
			switch(colName){
			case 'roleType':
				var html = '<div>'+
					'<a href="javascript:;" title="添加角色" class="otherBtn">'+
					    '<i class="icon icon-hand-right"></i>'+
					'</a>'+
				'</div>';
				/*var html = '<div>'+
				'<button class="btn kbtn tableDelbtn otherBtn">添加</button>'+
				'</div>';*/
			return html;
			case 'state':
				var dis = text=="1"?"启用":"禁用";
				var color = text=="1"?"green":"red";
				return '<span style="color:'+color+'">'+dis+'</span>';
			}
		},
		onAfterReady:function(){
			$(".otherBtn").click(function(e){
				var num = $(this).parents('tr').index();
				var record = staffRoleDatatable.records[num].data;
				var span = $(".rolesShowDiv p[name='"+record.roleId+"']");
				if(span == undefined || span == null || span.length == 0){
					//添加至div显示
					var html = '<p class="itemDiv" name="'+record.roleId+'">'+
					'<input style="display:none" value="'+record.roleId+'">'+
					'<span class="roleshow label label-warning">'+record.roleName+
						'<a href="javascript:;" title="删除" onclick="removeRole(\''+record.roleId+'\')">'+
						'<i class="icon icon-times"></i></a></span>'+
					'</p>';
					$(".rolesShowDiv").append(html);
				}
				e.stopPropagation();
				e.preventDefault();
			});
		}
};
function onRenderItem(colName,text){
	switch(colName){
	case 'state':
		var txt = (text=='0'||text==0)?'禁用':'正常';
		var color = (text=='0'||text==0)?'red':'green';
		return '<span style="color:'+color+'">'+txt+'</span>';
	case 'staffType':
		var html='<div class="tableRowsIcon">'+
					'<span title="修改"><a class="editABtn" href="javascript:;">'+
						'<i class="icon icon-edit"></i>'+
					'</a></span>'+
				'</div>';
		return html;	
	}

}
var staffIdBaseRole = "";
function editStaffInfo(){  //人员表点击
	$(".editABtn").on('click',function(){
		//获取当前行数据
		var num = $(this).parents('tr').index();
		var record = staffInfoDataTable.records[num].data;
		$("#staffBaseInfoFrm")[0].reset();
		//$(".rolesShowDiv").empty();
		$("#staffBaseInfoFrm").autofill(record);
		//展示角色
		staffIdBaseRole = record.staffId;
		$("#roleOldInfoFrm").submit();
		MsgBox.open("updateBase", "修改员工角色信息", ctx + "/sys/staffMgr/find/"+staffIdBaseRole+"/Roles", "800px", null, true);
	});
}

/*function onQueryRoleInfoListSucc(){
	var staffId = $("#staffIdByRoles").val();
	alert(staffId);
	findStaffRolesAjax.addParam("staffId",staffId);
	findStaffRolesAjax.send();
}

//查找员工角色后展示
var oldRoles = null;
function onFindStaffRoleSucc(data){
	if(data.success == true){
		oldRoles = data.info;
		console.log(oldRoles)
		var html = "";
		$.each(oldRoles,function(index,item){
			if(item != null){
				html += '<p class="itemDiv" name="'+item.roleId+'">'+
						'<input style="display:none" value="'+item.roleId+'">'+
						'<span class="roleshow label label-warning">'+item.roleName+
							'<a href="javascript:;" title="删除" onclick="removeRole(\''+item.roleId+'\')">'+
							'<i class="icon icon-times"></i></a></span>'+
					'</p>';
			}
		});
		//$(".rolesShowDiv").empty();
		//$(".rolesShowDiv").append(html);
		
		//MsgBox.trigger("", "", "#staffEditDialog");
	}
}*/

function saveAll(){
	var ids = new Array();
	var inputs = $(".rolesShowDiv input");
	$.each(inputs,function(index,item){
		ids.push($(this).val());
	});
	$("#staffHasRoles").val(ids);
	$("#staffBaseInfoFrm").submit();
}
function removeRole(id){
	$(".rolesShowDiv p[name='"+id+"'").remove();
}
function onSaveSucc(data){
	if(data.success == true){
		new $.zui.Messager('保存成功!', {
		    type: 'success',
		    time: 2000,
		    placement:'center'
		}).show();
	}
}
function closeDia(){
	MsgBox.closeActiveDialog();
}
function searchName(){
	roleMananagePagination.resetPagination();
	queryRoleInfoListAjax.send();
}

function  onDeleSucc(data){
	if(data.success==true){
		MsgBox.info(data.message,null,function(){
            staffInfoDataTable.refresh();
            MsgBox.closeActiveDialog();
		})
	}else{
        MsgBox.error(data.message,null,function(){
            staffInfoDataTable.refresh();
            MsgBox.closeActiveDialog();
        });
	}

}



function onSaveStaffSucc(){
    MsgBox.info("保存成功！",null,function(){
        var mWindow = MsgBox.getCurrentOpenDialogWindow();
        mWindow.staffInfoDataTable.refresh();
        MsgBox.closeActiveDialog();
    });
}


function onUpdateSucc() {
    MsgBox.info("更新成功！",null,function(){
        var mWindow = MsgBox.getCurrentOpenDialogWindow();
        mWindow.staffInfoDataTable.refresh();
        MsgBox.closeActiveDialog();
    });
}

//select 获取默认值
$(function(){
	var value = $("#staffSex2").attr("data-value");
	$("#staffSex2").val(value);
	var value2 = $("#state").attr("data-value");
	$("#state").val(value2);
})